<template>
  <div>
    <van-nav-bar
      fixed
      placeholder
      :title="`${$route.query.value}的搜索结果`"
      left-arrow
      @click-left="$router.back()"
    />
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="item in resultsList"
        :key="item.art_id"
        :title="item.title"
        @click="$router.push(`/detail/${item.art_id}`)"
      />
    </van-list>
  </div>
</template>

<script>
import { searchAPI } from '@/api/search'
export default {
  name: 'results',
  data () {
    return {
      resultsList: [],
      loading: false,
      finished: false,
      page: 1
    }
  },
  methods: {
    async onLoad () {
      const res = await searchAPI({
        page: this.page,
        per_page: 10,
        q: this.$route.query.value
      })
      // console.log(res)

      // 第一次进来查第一页
      // 页码+1，下次来就是查下一页
      this.page++
      this.resultsList.push(...res.data.results)
      // 把本次加载状态改为false
      this.loading = false
      // 判断是否全部加载完成
      if (this.resultsList.length >= res.data.total_count) {
        this.finished = true
      }
    }
  }
}
</script>

<style></style>
